package com.stps.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.stps.factory.ServiceFactory;
import com.stps.vo.NeedBook;

/**
 * Servlet implementation class NeedBookAddServlet
 */
@WebServlet("/NeedBookUpdateServlet")
public class NeedBookUpdateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public NeedBookUpdateServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");

		String crud = request.getParameter("crud");
		System.out.println("crud: "+crud);
		NeedBook NeedBook = new NeedBook();
		NeedBook.setId(Integer.valueOf(request.getParameter("id")));
		NeedBook.setName(request.getParameter("name"));
		NeedBook.setNum(Integer.valueOf(request.getParameter("num")));
		//System.out.println("NeedBook     "+NeedBook.toString());
		JSONObject jsonObj = new JSONObject();
		try {
			if(ServiceFactory.getNeedBookServiceFactory().update(NeedBook)) {
				jsonObj.put("code", 0);
				jsonObj.put("msg", "书籍信息修改成功....");
			}
			else {
				jsonObj.put("code", -1);
				jsonObj.put("msg", "书籍信息修改失败，请检查数据后重新提交....");
			}
				
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		response.setContentType("application/json;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.getWriter().append(jsonObj.toJSONString());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
